//定义DOM节点 
var addOne = document.getElementById("one");
var addSymbol = document.getElementById("add");
var addTwo = document.getElementById("two");
var btnDui = document.getElementById("dui");
var btnCuo = document.getElementById("cuo");
var wenhao = document.getElementById("wenhao");
var guanShu = document.getElementById("gs");
var s = document.getElementById("djs");
var musicDui = document.getElementById("musicDui");
var musicCuo = document.getElementById("musicCuo");
var guanShuNumber = guanShu.innerText;



function forceSafariPlayAudio() {
  //使用微信jssdk重新加载资源，解决IOS上Bug
  document.addEventListener("WeixinJSBridgeReady", function () {
    musicDui.load(); // iOS 9   还需要额外的 load 一下, 否则直接 play 无效
    musicCuo.load(); // iOS 9   还需要额外的 load 一下, 否则直接 play 无效
  }, false);

}
forceSafariPlayAudio();

//倒计时
var myinter = window.setInterval("run();", 1000);

function run() {
  s.innerHTML = s.innerHTML * 1 - 1;
  if (s.innerHTML <= -1) {
    clearInterval(myinter);
    guanShuNumber = guanShu.innerText;
    console.log("答错关数" + guanShuNumber);
    guanShuNumber = parseInt(guanShuNumber);
    musicCuo.play();
    alert("到时间啦!!" + "当前关数：" + guanShuNumber);
    s.innerHTML = 0;
    window.location.href = 'gameover.html';
    return false;
  }
}
//倒计时结束

//大于一定关数后倒计时变短函数
function timeShort(timeGuanShu, second) {
  if (guanShuNumber >= timeGuanShu) {
    s.innerHTML = second;
  } else {
    s.innerHTML = 4;
  }
}

//关数增加函数
function addguanshu() {
  guanShu.innerHTML = parseInt(guanShuNumber) + 1;
  guanShuNumber = guanShu.innerHTML;
  console.log("guanShu.innerText" + guanShu.innerText);
}

//答对时触发的函数
function answerTrue() {
  musicCuo.pause();
  musicDui.play();
  musicDui.currentTime = 0;
  mainFun();
  guanShuNumber = guanShu.innerText;
  addguanshu();
  timeShort(40, 2);
  run();
}
//答错误时触发的函数
function answerFalse() {
  guanShuNumber = guanShu.innerText;
  console.log("答错关数" + guanShuNumber);
  guanShuNumber = parseInt(guanShuNumber);
  musicDui.pause();
  musicCuo.play();
  musicCuo.currentTime = 0;
  alert("答错了!!" + "当前关数：" + guanShuNumber);
  btnCuo.disabled = "disabled";
  window.location.href = 'gameover.html';
}


//主函数
function mainFun() {
  var s = document.getElementById("djs");
  var ranNumber = parseInt(10 * Math.random());
  console.log("随机数" + ranNumber);
  //第一个随机数
  var value1 = parseInt(10 * Math.random());
  //第二个随机数
  var value2 = parseInt(10 * Math.random());

  //关数设置 不同关数时 两个相加数的值
  if (guanShuNumber >= 1 && guanShuNumber < 20) {
    value1 = parseInt(10 * Math.random());
    value2 = parseInt(10 * Math.random());
  } else if (guanShuNumber >= 20 && guanShuNumber < 40) {
    value1 = parseInt(10 * Math.random()) + parseInt(5 * ranNumber)+10;
    value2 = parseInt(10 * Math.random()) + parseInt(5 * ranNumber)+12;
  } else if (guanShuNumber >= 40 && guanShuNumber < 80) {
    value1 = parseInt(90 * ranNumber) + 105;
    value2 = parseInt(90 * ranNumber) + 100;
    ranNumber=ranNumber+1;
  } else if (guanShuNumber >= 80 && guanShuNumber < 1000) {
    value1 = parseInt(10 * Math.random()) + parseInt(100 * ranNumber)+101;
    value2 = parseInt(10 * Math.random()) + parseInt(100 * ranNumber)+109;
  }
  //设置两个相加数的值
  addOne.innerHTML = value1;
  addTwo.innerHTML = value2;
  //正确答案


  //增加减法---通过判断关数模式
  // if (guanShuNumber > 10 && guanShuNumber < 18) {
  //   addSymbol.innerHTML = "-";
  //   ans = value1 - value2;
  //   wenhao.innerHTML = ans - ranNumber;
  //   console.log("减法：" + wenhao.innerHTML);
  // } else {
  //   console.log("else else")
  //   addSymbol.innerHTML = "+";
  //   ans = value1 + value2;
  //   wenhao.innerHTML = ans + ranNumber;
  // }

  //增加减法---通过判断随机数模式
  if (ranNumber > 5 && ranNumber < 10) {
    addSymbol.innerHTML = "-";
    ans = value1 - value2;
    wenhao.innerHTML = ans - ranNumber;
    console.log("减法：" + wenhao.innerHTML);
  } else {
    console.log("else else")
    addSymbol.innerHTML = "+";
    ans = value1 + value2;
    wenhao.innerHTML = ans + ranNumber;
  };

  //设置正确率 当随机出7、4、9时，重置为0，即正确答案
  if (ranNumber >= 4) {
    ranNumber = 0;
    //wenhao.innerHTML = ans + ranNumber;
  }
 

  //设置问号处的值
  wenhao.innerHTML = ans + ranNumber;
  console.log(value1);
  console.log(value2);
  btnDui.onclick = function () {
    if (ans == ans + ranNumber) {
      //alert("答对了");
      answerTrue();
    } else {
      //答错了
      answerFalse();
    }
  };
  btnCuo.onclick = function () {
    if (ans == ans + ranNumber) {
      //答错了
      answerFalse();
    } else {
      //alert("答对了");
      answerTrue();
    }
  }

}
//主函数结束

mainFun();